#include "TableWidgetForm.h"
#include "ui_TableWidgetForm.h"
#include "NumberDelegate.h"
#include <QMenu>
#include <QMessageBox>

TableWidgetForm::TableWidgetForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TableWidgetForm)
{
    ui->setupUi(this);
    this->setMinimumHeight(150);
    ui->tableWidget->resizeColumnsToContents();
    //ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//行选中
    //ui->tableWidget->setAlternatingRowColors(true);
    //ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//横向先自适应宽度
    ui->tableWidget->horizontalHeader()->setDefaultSectionSize(300);

    // 连接右键菜单信号
    connect(ui->tableWidget, &QTableWidget::customContextMenuRequested, this, &TableWidgetForm::showContextMenu);
}

TableWidgetForm::~TableWidgetForm()
{
    delete ui;
}

void TableWidgetForm::setGroupBoxTitle(const QString &Title)
{
    ui->groupBox->setTitle(Title);
}

void TableWidgetForm::setTableWidgetHeaderLabels(const QStringList &labels)
{
    ui->tableWidget->setColumnCount(labels.size());
    ui->tableWidget->setHorizontalHeaderLabels(labels);
}

void TableWidgetForm::setTableWidgetHeaderType(const QStringList &labels)
{
    for (int i = 0;i<labels.size();i++)
    {
        if(labels.at(i) == "i")
        {
            NumberDelegate* delegate = new NumberDelegate(0, ui->tableWidget);
            ui->tableWidget->setItemDelegateForColumn(i, delegate);
        }
        else if(labels.at(i) == "d")
        {
            NumberDelegate* delegate = new NumberDelegate(1, ui->tableWidget);
            ui->tableWidget->setItemDelegateForColumn(i, delegate);
        }
    }
}

void TableWidgetForm::setEnableAddAndDel(bool isenable)
{
    if(isenable)
    {
        ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    }
    else {
        ui->tableWidget->setContextMenuPolicy(Qt::NoContextMenu);
    }
}

void TableWidgetForm::setTablecolumnhide(int index)
{
    ui->tableWidget->setColumnHidden(index, true);
}

void TableWidgetForm::setTablecolumndisEdit(int index)
{
    for(int i = 0;i<ui->tableWidget->rowCount();i++)
    {
        QTableWidgetItem* item = ui->tableWidget->item(i,index);
        if(item)
        {
            item->setFlags(item->flags()&~Qt::ItemIsEnabled);
        }
    }
}

QTableWidget *TableWidgetForm::getTableWidget()
{
    return ui->tableWidget;
}

void TableWidgetForm::showContextMenu(const QPoint &pos)
{
    QModelIndex index = ui->tableWidget->indexAt(pos);

    QMenu contextMenu;

    QAction *actionEdit = contextMenu.addAction("增加");
    QAction *actionDelete = contextMenu.addAction("删除");

    connect(actionEdit, &QAction::triggered, this, [this]() {
        //遍历 获取最大的ID
        int maxID = 0;
        int rowCount = ui->tableWidget->rowCount();
        for (int i= 0;i < rowCount;i++)
        {
            if(ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt() > maxID)
                maxID = ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
        }
        ui->tableWidget->setRowCount(rowCount+1);
        ui->tableWidget->setItem(rowCount, 0, new QTableWidgetItem(""));
        ui->tableWidget->item(rowCount,0)->setData(Qt::UserRole, maxID+1);
    });
    connect(actionDelete, &QAction::triggered, this, [this, index]() {
        ui->tableWidget->removeRow(index.row());
    });

    contextMenu.exec(ui->tableWidget->viewport()->mapToGlobal(pos));
}
